﻿
using System.Windows;
using System.Windows.Media;

namespace Cyl.ImageControl;

public class CircleROI : ROI
{
    public double Radius => Width / 2;

    public CircleROI() { }

    public CircleROI(Point center, double radius, Color color)
    {
        Bounds = new Rect(new Point(center.X - radius, center.Y - radius), new Size(radius * 2, radius * 2));
        Color = color;
    }

    public override void Draw(DrawingContext dc, double scale, Point offset)
    {
        ApplyTransform(dc, scale, offset);
        dc.DrawEllipse(null, new Pen(new SolidColorBrush(Color), 2), Center, Radius, Radius);

        // 绘制圆心标记
        var centerMarkRadius = 5 / scale; // 圆心标记的半径
        dc.DrawEllipse(Brushes.Yellow, null, Center, centerMarkRadius, centerMarkRadius);

        dc.Pop();
    }

    public override Point[] GetControlPoints()
    {
        return new Point[]
        {
                new Point(Center.X + Radius, Center.Y) // Right
        };
    }

    public override void UpdateControlPoint(Point controlPoint, Point newPosition)
    {
        var centerX = Center.X;
        var centerY = Center.Y;

        if (controlPoint == new Point(centerX + Radius, centerY)) // Right
        {
            var newRadius = Math.Abs(newPosition.X - centerX);
            Bounds = new Rect(new Point(centerX - newRadius, centerY - newRadius), new Size(newRadius * 2, newRadius * 2));
        }
    }

    public override void Move(Point offset)
    {
        Bounds = new Rect(new Point(Bounds.X + offset.X, Bounds.Y + offset.Y), Bounds.Size);
    }
}
